<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\wh\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\mill\model\Client as ClientModel;
use app\wh\model\StockType as StockTypeModel;
use think\Db;
use think\facade\Env;

/**
 * 仓库控制器
 * @package app\admin\controller
 */
class Index extends Admin
{
    //首页
    public function index(){

        $stocktype = StockTypeModel::getGroup();

        $extend_css_list[] = PUBLIC_PATH. 'static/wh/css/index.css';

        $style = '<style>
            .block-height-1{
                height: 315px;
            }
            .block-height-2{
                height: 115px;
            }
            .wh-date {
                color: #999999;
            }
            .wh-date-active {
                color: #000;
            }
            .wh-stat-1{
                color: #000;
                padding: 0 15px;
            }
            .wh-stat-head{
                height: 65px;
                margin-top: 5px;
                padding: 10px 4px 0;
            }
            .wh-stat-head2{
                //height: 37px;
                //margin-top: 5px;
                padding: 0;
            }
            .wh-stat-head2-content{
                margin-top: 0;
            }
            .wh-stat-head-text{
                background-color: #fff;
                border-color: #fff;
                font-size: 18px;
                text-align: left;
                padding-left: 0;
            }
            .wh-stat {
                height: 220px;
                display: table;
                padding: 0;
                margin: 0 10px;
            }
            .wh-stat-body {
                display: table-cell;
                vertical-align: middle;
                width: 100%;
                text-align: center;
            }
            .wh-stat-body>p {
                padding: 0;
                margin-bottom: 0;
            }
            .wh-stat-title {
                font-weight: 600;
                font-size: 24px;
                margin-bottom: 15px;
            }
            .wh-stat-border {
                border: 2px solid;
            }
            .wh-stat-border-jb {
                float: left;
                margin-right: -14px;
                margin-top: 1px;
                margin-left: 1px;
                width: 0;
                border-top-width: 15px;
                border-right-width: 13px;
                border-left-width: 0;
                border-bottom-width: 0;
                border-color: #000000 transparent transparent transparent;
                border-style: solid;
            }
            .wh-stat-type-c {
                border-bottom: 15px solid #f1f1f1;
                padding: 10px 20px 35px 20px;
            }
            .wh-stat-type-c-p {
                color: #999999;
                font-size: 14px;
            }
            .wh-stat-type-c-h1 {
                color: #545454;
                font-size: 18px;
                font-weight: 600;
            }
            .wh-stat-type-c-li {
                vertical-align: middle;
                height: 34px;
                border-bottom: 1px solid #f1f1f1;
                padding: 10px 20px 35px 20px;
            }
            .wh-stat-type-c-li-t {
                color: #999999;
                font-size: 16px;
                display: table-cell;
                float: left
            }
            .wh-stat-type-c-li-p {
                color: #000;
                font-size: 16px;
                display: table-cell;
                float: right
            }
            .wh-stat-2 {
                height: inherit;
                display: table;
                padding: 0;
                margin: 0 10px;
            }
            .wh-stat-2-content {
                display: flex;
                justify-content: center;
                align-items: center;
            }
            .wh-stat-2-h1 {
                font-size: 28px
            }
            .wh-stat-2-p {
                font-size: 15px
            }
            .wh-stat-2-img {
                width: 36px
            }
            .wh-stat-2-border {
                border-left: 1px solid #e5e9ed;
            }
            .wh-stat-border-left {
                width: 6px;
                height: 35%;
                background: #7d8afe;
                float: left;
                position: relative;
                top: 35%;
                border-radius: 10px;
                margin-left: -3px;
            }
            .wh-stat-2-ul {
                display: flex;
                justify-content: unset;
                align-items: center;
                background-color: #f9faff;
                height: 35px;
                overflow: hidden;
                margin-bottom: 5px
            }
            .wh-stat-2-li {
                color: #999999;
                font-size: 16px;
                display: table-cell;
                float: left;
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
            }
            .wh-stat-2-li-label {
                color: #7d8afe;
                font-size: 16px;
                display: table-cell;
                float: left;
                margin: 0 5px 0 0;
                vertical-align: middle;
            }
            /*-----------------------stat color-------------------*/
            .wh-stat-border-color-1 {
                border-color: #977deb;
            }
            .wh-stat-border-color-1>.wh-stat-border-jb {
                border-color: #977deb transparent transparent transparent;
            }
            .wh-stat-border-color-1>.wh-stat-body>.wh-stat-color {
                color: #977deb;
            }
            
            .wh-stat-border-color-2 {
                border-color: #f97187;
            }
            .wh-stat-border-color-2>.wh-stat-border-jb {
                border-color: #f97187 transparent transparent transparent;
            }
            .wh-stat-border-color-2>.wh-stat-body>.wh-stat-color {
                color: #f97187;
            }
            
            .wh-stat-border-color-3 {
                border-color: #73f094;
            }
            .wh-stat-border-color-3>.wh-stat-border-jb {
                border-color: #73f094 transparent transparent transparent;
            }
            .wh-stat-border-color-3>.wh-stat-body>.wh-stat-color {
                color: #73f094;
            }
            
            .wh-stat-border-color-4 {
                border-color: #42e4d5;
            }
            .wh-stat-border-color-4>.wh-stat-border-jb {
                border-color: #42e4d5 transparent transparent transparent;
            }
            .wh-stat-border-color-4>.wh-stat-body>.wh-stat-color {
                color: #42e4d5;
            }
            
            .wh-stat-border-color-5 {
                border-color: #f1cda7;
            }
            .wh-stat-border-color-5>.wh-stat-border-jb {
                border-color: #f1cda7 transparent transparent transparent;
            }
            .wh-stat-border-color-5>.wh-stat-body>.wh-stat-color {
                color: #f1cda7;
            }
            
            .wh-stat-border-color-6 {
                border-color: #03bfab;
            }
            .wh-stat-border-color-6>.wh-stat-border-jb {
                border-color: #03bfab transparent transparent transparent;
            }
            .wh-stat-border-color-6>.wh-stat-body>.wh-stat-color {
                color: #03bfab;
            }
            
        </style>';
        $this->assign('style',$style);
        //$this->assign('extend_css_list',$extend_css_list);
        $this->assign('stocktype',$stocktype);
        return $this->fetch();
    }
    /**
     * 客户列表页
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function client()
    {
        cookie('__forward__', $_SERVER['REQUEST_URI']);

        // 获取查询条件
        $map = $this->getMap();
        // 数据列表
        $data_list = Db::name('mill_client')
            ->alias('c')
            ->field('c.*')
            ->leftJoin('mill_order o','o.client_id=c.id')
            ->where($map)
            ->group('c.id')
            ->order('c.id desc')
            ->paginate();

        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('客户列表（客户数量'.$data_list->total().'）') // 页面标题
            ->setTableName('mill_client') // 设置表名
            ->hideCheckbox()
            ->setSearchArea([
                ['text:2', 'c.company', '客户公司', 'like'],
                ['text:2', 'c.mobile', '客户联系电话', 'like'],
                ['text:1', 'c.back', '内勤人员', 'like'],
                ['text:2', 'c.sales', '销售经理', 'like'],
                ['daterange:2', 'c.create_time', '日期'],
            ])
            ->addColumns([ // 批量添加列
                ['id', '客户序号'],
                ['company', '客户公司'],
                ['nickname', '客户联系人'],
                ['mobile', '客户联系电话'],
                ['address', '客户地址'],
                ['sales', '销售经理'],
                ['sales_mobile', '销售电话'],
                ['back', '内勤人员'],
                //['create_time', '添加日期','date'],
                ['note', '备注','popover'],
                ['right_button', '操作', 'btn']
            ])
            //->addColumn('back', '类型', 'select', ['a' =>'类型A','b' => '类型B','c' => '类型C'])
            ->addTopButtons('add') // 批量添加顶部按钮
            ->addRightButtons('edit') // 批量添加右侧按钮
            ->addRightButton('order', ['title' => '添加订单', 'href' => url('mill/order/add', [ 'client_id' => '__id__']), 'icon' => ''])
            ->addRightButton('order', ['title' => '查看订单', 'href' => url('mill/order/index')."?_s=client_id=__id__&_o=client_id=eq", 'icon' => ''])
            ->setRowList($data_list) // 设置表格数据
            ->setColumnWidth([
                'id'  => 60,
                'note'  => 100,
                'mobile'  => 80,
                'sales_mobile'  => 80,
                'address'  => 150,
            ])
            ->fetch(); // 渲染模板
    }

    /**
     * 新增
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = input();
            // 验证
            $result = $this->validate($data, 'Client');
            // 验证失败 输出错误信息
            if(true !== $result) $this->error($result);

            $data['create_time'] = empty($data['create_time'])?time():strtotime($data['create_time']);
            $data['update_time'] = time();
            if (ClientModel::create($data)) {
                $this->success('操作成功', url('index'));
            }
            $this->error('操作失败');
        }


        return ZBuilder::make('form')
            ->setPageTitle('新增客户') // 页面标题
            ->addFormItems([ // 批量添加表单项
                ['text:||||must', 'nickname', '客户联系人'],
                ['text:||||must', 'sales', '销售经理'],
                ['text:||||must', 'company', '客户公司名称'],
                ['text:||||must', 'sales_mobile', '手机电话'],
                ['text:||||must', 'address', '客户地址'],
                ['text:||||must', 'back', '内勤人员'],
                ['text:||||must', 'mobile', '客户联系电话'],
                ['date:||||must', 'create_time', '日期'],
                ['textarea', 'note', '备注'],
            ])
            ->layout(['note'=>12])
            ->fetch();
    }

    /**
     * 编辑
     * @param null $id 客户id
     * @return mixed
     */
    public function edit($id=null)
    {

        $id = input('param.id');
        // 获取数据
        $info = ClientModel::find($id);

        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Client');
            // 验证失败 输出错误信息
            if(true !== $result) $this->error($result);

            $data['create_time'] = empty($data['create_time'])?$info['create_time']:strtotime($data['create_time']);
            $data['update_time'] = time();
            // 更新数据
            if (ClientModel::update($data)) {
                $this->success('编辑成功', cookie('__forward__'));
            } else {
                $this->error('编辑失败');
            }
        }

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('编辑客户')
            ->addFormItem('hidden', 'id')
            ->addFormItems([ // 批量添加表单项
                ['text:||||must', 'nickname', '客户联系人'],
                ['text:||||must', 'sales', '销售经理'],
                ['text:||||must', 'company', '客户公司名称'],
                ['text:||||must', 'sales_mobile', '手机电话'],
                ['text:||||must', 'address', '客户地址'],
                ['text:||||must', 'back', '内勤人员'],
                ['text:||||must', 'mobile', '客户联系电话'],
                ['date:||||must', 'create_time', '日期'],
                ['textarea', 'note', '备注'],
            ])
            ->setFormData($info)
            ->layout(['note'=>12])
            ->fetch();
    }

    /**
     * 删除
     * @param array $record 行为日志
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delete($record = [])
    {
        return $this->setStatus('delete');
    }

    /**
     * 启用
     * @param array $record 行为日志
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function enable($record = [])
    {
        return $this->setStatus('enable');
    }

    /**
     * 禁用
     * @param array $record 行为日志
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function disable($record = [])
    {
        return $this->setStatus('disable');
    }

    /**
     * 设置客户状态：删除、禁用、启用
     * @param string $type 类型：delete/enable/disable
     * @param array $record
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function setStatus($type = '', $record = [])
    {
        $ids = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        $ids = (array)$ids;

        foreach ($ids as $id) {

            switch ($type) {
                case 'enable':
                    if (false === ClientModel::where('id', $id)->setField('status', 1)) {
                        $this->error('启用失败，客户ID：'.$id);
                    }
                    break;
                case 'disable':
                    if (false === ClientModel::where('id', $id)->setField('status', 0)) {
                        $this->error('禁用失败，客户ID：'.$id);
                    }
                    break;
                case 'delete':

                    if (false === ClientModel::where('id', 'in', $id)->delete()) {
                        $this->error('删除失败，客户ID：'.$id);
                    }
                    break;
                default:
                    $this->error('非法操作');
            }

            action_log('role_'.$type, 'mill', $id, UID);
        }

        $this->success('操作成功');
    }
}
